package com.bskj.kuaifu.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.bskj.common.constants.CommonConstatns;
import com.bskj.common.utils.EncryptUtil;
import com.bskj.common.utils.WeixinApiUtils;
import com.bskj.common.web.BaseController;
import com.bskj.kuaifu.bean.vo.req.NoCarnoReqVo;
import com.bskj.kuaifu.service.INoCarnoParkInfoService;

/**
 * 无牌车停车相关
 * 
 * @author lishi
 * @date 2018年5月2日 下午3:09:56
 */
@Controller
@RequestMapping("noCarnoParkInfo")
public class NoCarnoParkInfoController extends BaseController {

	@Autowired
	private INoCarnoParkInfoService noCarnoParkInfoService;

	/**
	 * 无牌车进场
	 *
	 * @param reqVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("carIn")
	public ModelAndView carIn(NoCarnoReqVo reqVo) throws Exception {
		ModelAndView mv = new ModelAndView();
		Map<String, Object> resultMap = noCarnoParkInfoService.carIn(reqVo);
		boolean result = (boolean) resultMap.get("result");
		mv.addObject("result", result);
		if (result) {
			mv.addObject("parkLotName", resultMap.get("parkLotName"));
			mv.addObject("inTime", resultMap.get("inTime"));
		} else {
			mv.addObject("msg", resultMap.get("msg"));
		}
		mv.setViewName("/parkLot/in");
		return mv;
	}

	/**
	 * 无牌车出场
	 *
	 * @param reqVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("carOut")
	public ModelAndView carOut(NoCarnoReqVo reqVo, String url) throws Exception {
		ModelAndView mv = new ModelAndView();

		Map<String, Object> resultMap = noCarnoParkInfoService.carOut(reqVo);
		boolean result = (boolean) resultMap.get("result");
		mv.addObject("result", result);
		if (result) {
			mv.addObject("parkLotName", resultMap.get("parkLotName"));
			mv.addObject("inTime", resultMap.get("inTime"));
			mv.addObject("outTime", resultMap.get("outTime"));
			mv.addObject("charge", resultMap.get("charge"));

			//统一下单参数
			JSONObject unifiedorderParams = new JSONObject();
			unifiedorderParams.put("uri", "/pay/noCarnoUnifiedorder");
			String openId = getRequest().getParameter("openId");
			unifiedorderParams.put("openId", openId);
			unifiedorderParams.put("parkId", resultMap.get("parkId"));
			mv.addObject("unifiedorderParams", EncryptUtil.encrypt(unifiedorderParams.toJSONString()));

			//jsticket
			JSONObject signatureObj = WeixinApiUtils.jssdk_signture(url, CommonConstatns.KUAIFU_ACCOUNTID);
			mv.addObject("signature", signatureObj);
		} else {
			mv.addObject("msg", resultMap.get("msg"));
		}
		mv.setViewName("/parkLot/out");
		return mv;
	}

	@ResponseBody
	@RequestMapping("register")
	public Map<String, Object> register(String boxNo, String boxName, Integer parkLotId, String parkLotUuid,
			String rails) throws Exception {
		return noCarnoParkInfoService.registerBox(boxNo, boxName, parkLotId, parkLotUuid, rails);
	}
}
